package com.yu.shaungxuan.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JwtUtil {

    public static final String singKey = "health";
    public static final Long expire = 1800000*48L;

    /**
     * 生成JWT令牌
     * @param claims
     * @return
     */
    public static String generateJwt(Map<String,Object> claims){
        return Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, singKey)
                .setExpiration(new Date(System.currentTimeMillis()+expire))
                .compact();
    }

    public static String createJWT(String adminSecretKey,long adminTtl,Map<String,Object> claims){
        return Jwts.builder()
                .addClaims(claims)
                .signWith(SignatureAlgorithm.HS256, adminSecretKey)
                .setExpiration(new Date(System.currentTimeMillis()+adminTtl))
                .compact();
    }


    public static Claims parseJWT(String secretKey,String token){
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(token)
                .getBody();
    }


}